﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Navigation;
using Zzj.Model;
using Zzj.Pages;
using Zzj.Utils;

namespace Zzj.Utils
{
    /// <summary>
    /// 继承所有的读卡器读取身份证
    /// </summary>
    public class IdCardRead
    {
        /// <summary>
        /// 读取身份证信息
        /// </summary>
        /// <returns> </returns>
        public static IDCardInfo ReadCardInfo()
        {
            IDCardInfo iDCardInfo = new IDCardInfo();
            string cardDevice = (string)App.getConfig().IDCardDevice;
            string CardReaderDeviceType = App.getConfig().CardReaderDeviceType;//获取使用的读卡器品牌

            switch (int.Parse(App.getConfig().CardReaderDeviceType))
            {
                case 0://配置文件
                    iDCardInfo = defaultConfigurationFileReading();
                    break;

                case 1: // 神思
                    iDCardInfo = SSReadCardInfo();
                    break;

                case 2: // 精伦
                    iDCardInfo = JLReadCardInfo();
                    break;

                case 3: // 熵机
                    iDCardInfo = IDCardReaderUtil_Sjdll.ReadCard();
                    break;

                default:
                    break;
            }

            return iDCardInfo;
        }

        /// <summary>
        /// 初始化读卡器
        /// </summary>
        public static void cardReaderInitialization()
        {
            switch (int.Parse(App.getConfig().CardReaderDeviceType))
            {
                case 1: // 神思
                    string port = App.getConfig().SSIDCardPort;
                    string rate = App.getConfig().SSIDCardPortBaudRate;
                    int handle = CommonInterface.OpenDevice(port, rate, "");
                    if (handle <= 0)
                    {
                        MessageBox.Show("打开设备失败，错误代码：" + handle.ToString());
                    }
                    break;

                case 2: // 精伦
                    int code = JLIDcardread.Opendevice();//打开读卡设备
                    if (code != 1)
                    {
                        MessageBox.Show("打开设备失败");
                    }
                    break;

                case 3: // 熵机
                    string IDCardPort = App.getConfig().SJIDCardPort;
                    int inport = Convert.ToInt32(IDCardPort);
                    bool inPortType = IDCardReaderUtil_Sjdll.InitDevice(inport);
                    if (!inPortType)
                    {
                        MessageBox.Show("熵机读卡器设备打开失败");
                    }
                    break;

                default:
                    break;
            }
        }

        /// <summary>
        /// 关闭读卡器
        /// </summary>
        public static void turnOffCardReader()
        {
            switch (int.Parse(App.getConfig().CardReaderDeviceType))
            {
                case 1: // 神思

                    break;

                case 2: // 精伦
                    try
                    {
                        JLIDcardread.closeID();//关闭设备
                    }
                    catch { }
                    break;

                case 3: // 熵机
                    bool code = IDCardReaderUtil_Sjdll.CloseDevice();
                    if (!code)
                    {
                        MessageBox.Show("设备关闭失败");
                    }
                    break;

                default:
                    break;
            }
        }

        /// <summary>
        /// 神思读卡器读取信息,返回身份证号
        /// </summary>
        public static IDCardInfo SSReadCardInfo()
        {
            IDCardInfo iDCardInfo = new IDCardInfo();
            StringBuilder IdCardInfoBuilder = new StringBuilder(409600);
            int ret = CommonInterface.SdtReadCard(0, 1, IdCardInfoBuilder, 0);
            if (ret == 0)
            {
                string[] CardInfo = IdCardInfoBuilder.ToString().Split(':');
                String name = CardInfo[1];
                String idNum = CardInfo[9];
                iDCardInfo.IDNumber = idNum;//身份证号
            }
            return iDCardInfo;
        }

        /// <summary>
        /// 精伦读卡获取信息
        /// </summary>
        /// <returns> </returns>
        public static IDCardInfo JLReadCardInfo()
        {
            IDCardInfo iDCardInfo = JLIDcardread.ReadIDcardEnhanced();

            return iDCardInfo;
        }

        public static IDCardInfo defaultConfigurationFileReading()
        {
            IDCardInfo iDCardInfo = new IDCardInfo();
            iDCardInfo.IDNumber = App.getConfig().IDCardCode;
            iDCardInfo.MacCard = App.getConfig().MacCard;
            return iDCardInfo;
        }
    }
}